package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
)

type Hostinfo struct {
	Id   int    `json:"id"`
	Host string `json:"host"`
}

type Params struct {
	ReportData string `json:"reportData"`
}

func main() {
	a := []Hostinfo{
		Hostinfo{
			Id:   1,
			Host: "127.0.0.11",
		},
	}
	pp, _ := json.Marshal(a)
	par := Params{
		ReportData: string(pp),
	}

	var userinfo string = fmt.Sprintf(`{ "username": "admin","password": "admin"}`)
	data := make(map[string]string)

	if req, err := http.NewRequest("POST", "http://127.0.0.1:9090/api-token-auth/", strings.NewReader(userinfo)); err != nil {
		log.Println(err)

	} else {
		client := &http.Client{}
		req.Header.Set("Content-Type", "application/json")
		res, _ := client.Do(req)
		r, _ := ioutil.ReadAll(res.Body)
		_ = json.Unmarshal(r, &data)
		d, _ := json.MarshalIndent(par, "", "    ")

		log.Println(string(d))
		if req, err := http.NewRequest("PUT", "http://127.0.0.1:9090/api/HtmlConfig/7/", strings.NewReader(string(d))); err != nil {
			log.Println(err)

		} else {
			req.Header.Set("Content-Type", "application/json")
			req.Header.Set("Authorization", "JWT "+data["token"])
			res, _ := client.Do(req)

			r, _ := ioutil.ReadAll(res.Body)
			log.Println(string(r))

		}

	}

}
